package nl.hva.three2learn.controller
{
	import flash.events.MouseEvent;
	import flash.media.Camera;
	import flash.net.NetStream;

	import mx.core.FlexGlobals;
	import mx.core.Application;
	import mx.core.INavigatorContent;

	import mx.controls.Alert;

	import nl.hva.three2learn.Variables;
	import nl.hva.three2learn.ui.Line;
	import nl.hva.three2learn.view.ChatView;

	public class ChatController
	{
		[Bindable]
		public var view:ChatView;

		[Bindable]
		public var sessionName:String;

		private var _appHandler:ApplicationHandler;

		public function ChatController()
		{
			if (Variables.isTeacher)
				_appHandler = new TeacherApplicationHandler(this);
			else
				_appHandler = new StudentApplicationHandler(this);
		}

		public function creationComplete_handler():void
		{
			_appHandler.doInit();
		}
		
		public function txtInput_enterHandler():void
		{
			var message:String = view.txtInput.text;

			_appHandler.sendMessage(message);
			appendMessage(Variables.name, message);

			view.txtInput.text = "";
		}

		public function btnSend_clickHandler():void
		{
			txtInput_enterHandler();
		}

		public function blackboardUpdated(lines:Array):void
		{
			if (lines.length === 0)
				view.blackboard.clear();
			else
				view.blackboard.addLine(new Line(lines), !Variables.isTeacher);
		}

		public function appendMessage(sender:String, message:String):void
		{
			view.txtChat.appendText("[" + sender + "] " + message + "\n");
		}

		public function attachCamera(camera:Camera):void
		{
			view.videoContainer.attachCamera(camera);
		}

		public function attachNetStream(ns:NetStream):void
		{
			view.videoContainer.attachNetStream(ns);
		}
	}
}
